package org.redmaple.core.retrieving;

import org.redmaple.core.support.PropertyNameFormatException;
import org.redmaple.core.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 属性名称解析器，从Controller传到Service的检索信息中，所有的检索属性名称上会包含检索类型(不带检索类型则为默认的检索类型)，默认为EQ，
 * 属性名称取以 <code>propertyPrefixPre</code> 开始，以
 * <code>propertyPrefixPost</code> 结束之间的字符串为检索类型<br/>
 * 可在Spring配置中进行对 propertyPrefixPre 与 propertyPrefixPost 的设置,如下：<br/>
 * &lt;bean id="servicePropertyResolver"
 * class="org.redmaple.core.retrieving.PropertyNameResolver"&gt;<br/>
 * &emsp;&lt;property name="propertyPrefixPre" value="@" /&gt;<br/>
 * &emsp;&lt;property name="propertyPrefixPost" value="@" /&gt;<br/>
 * &lt;/bean&gt;
 * 
 * @author Xiangyun
 *
 */
public class PropertyNameResolver {

	protected Logger logger = LoggerFactory
			.getLogger(PropertyNameResolver.class);

	private static final String THROW_RESOLVER_ERROR_MESSAGE = "属性格式不合法，无法进行解析";

	/**
	 * 属性前缀的前标识字符，默认为@
	 */
	private String propertyPrefixPre = "@";

	/**
	 * 属性前缀的后标识字符，默认为@
	 */
	private String propertyPrefixPost = "@";

	public void setPropertyPrefixPre(String value) {
		logger.info("加载Service属性前缀的前标识符为: " + value);
		propertyPrefixPre = value;
	}

	public void setPropertyPrefixPost(String value) {
		logger.info("加载Service属性前缀的后标识符为: " + value);
		propertyPrefixPost = value;
	}
	
	public String getPropertyPrefixPre() {
		return propertyPrefixPre;
	}
	
	public String getPropertyPrefixPost() {
		return propertyPrefixPost;
	}

	public String getPropertyPrefix(RetrievingType retrievingType) {
		return new StringBuffer().append(propertyPrefixPre)
				.append(retrievingType.getPrefix())
				.append(propertyPrefixPost).toString();
	}

	private RetrievingType formatPropertyPrefix(String propertyPrefix)
			throws PropertyNameFormatException {
		int startIndex = propertyPrefixPre.length();
		int endIndex = propertyPrefix.indexOf(propertyPrefixPost, startIndex);
		if (endIndex == -1 || startIndex == endIndex) {
			throw new PropertyNameFormatException(THROW_RESOLVER_ERROR_MESSAGE);
		}
		return RetrievingType.valueOf(propertyPrefix.substring(startIndex,
				endIndex));
	}

	public RetrievingType getRetrievingType(String filterProperty) {
		Assert.hasText(filterProperty, "属性名称不能为空");
		if (!filterProperty.startsWith(propertyPrefixPre)) {
			return RetrievingType.EQ;
		}
		try {
			return formatPropertyPrefix(filterProperty);
		} catch (PropertyNameFormatException e) {
			logger.error(THROW_RESOLVER_ERROR_MESSAGE);
			e.printStackTrace();
			return RetrievingType.EQ;
		}
	}

	public String getPropertyName(String filterProperty) {
		int startIndex = filterProperty.indexOf(propertyPrefixPre);
		int endIndex = filterProperty.indexOf(propertyPrefixPre, startIndex);
		if (startIndex == -1 || endIndex == -1 || startIndex == endIndex) {
			return filterProperty;
		} else {
			return filterProperty.substring(endIndex);
		}
	}
}
